﻿using GrapeCity.Forguncy.Commands;
using GrapeCity.Forguncy.Plugin;
using Newtonsoft.Json.Linq;
using RestSharp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace YouDao
{
    [Icon("pack://application:,,,/YouDao;component/Resources/Icon.png")]
    public class YouDao : Command, ICommandExecutableInServerSide
    {
        //重写插件名称
        public override string ToString()
        {
            return "有道翻译";
        }
        //该插件只能在服务端命令中使用
        public override CommandScope GetCommandScope()
        {
            return CommandScope.ServerSide;
        }

        [DisplayName("原文")]
        [Description("待翻译的内容")]
        [FormulaProperty(true)]
        public object Src { get; set; }

        [DisplayName("翻译类型")]
        [Description("默认AUTO为自动翻译为中文，此外还支持类型 类似于ZH_CN2EN或者EN2ZH_CN")]
        [FormulaProperty(true)]
        public object Type { get; set; }



        public ExecuteResult Execute(IServerCommandExecuteContext dataContext)
        {
            try
            {
                //request
                string Src = dataContext.EvaluateFormula(this.Src).ToString();
                string Type = dataContext.EvaluateFormula(this.Type).ToString();

                var baseUrl= "http://fanyi.youdao.com/translate?&doctype=json&type=" + "AUTO" + "&i=" + Src;
                //url
                if (Type != null)
                {
                    baseUrl = "http://fanyi.youdao.com/translate?&doctype=json&type=" + Type + "&i=" + Src;
                }

                Console.WriteLine(baseUrl);
                var client = new RestClient(baseUrl);
                var request = new RestRequest(Method.POST);
                IRestResponse response = client.Execute(request);
                var res = response.Content;

                var result = JObject.Parse(res).GetValue("translateResult");
                string tgt = result[0][0]["tgt"].ToString();


                if (response.StatusCode == HttpStatusCode.OK)
                {
                    return new ExecuteResult()
                    {
                        ErrCode = 0,
                        Message = tgt,
                        AllowWriteResultToResponse = true
                    };
                }
                else
                {
                    return new ExecuteResult()
                    {
                        ErrCode = 2,
                        Message = response.StatusCode.ToString()+response.Content.ToString(),
                        AllowWriteResultToResponse = true
                    }; 
                   
                }
            }
            catch (Exception e)
            {
                return new ExecuteResult()
                {
                    ErrCode = 1,
                    Message = e.Message,
                    AllowWriteResultToResponse = true
                };
            }
        }
        public enum TypeEnum
        {
            AUTO,
            ZH_CN2EN,
            ZH_CN2JA,
            ZH_CN2KR,
            ZH_CN2FR,
            ZH_CN2RU,
            ZH_CN2SP,
            EN2ZH_CN,
            JA2ZH_CN,
            KR2ZH_CN,
            FR2ZH_CN,
            RU2ZH_CN,
            SP2ZH_CN

        }
    }

}
